#include "../mqserver/mq_binding.hpp"
#include <gtest/gtest.h>
ns_kkymq::BindingManager::ptr bmp;

class BindingTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        bmp = std::make_shared<ns_kkymq::BindingManager>("./data/meta.db");
    }
    virtual void TearDown() override
    {
        // emp->clear();
    }
};

TEST(binding_test, insert_test)
{
    bmp->bind("exchange1", "queue1", "new.music.#", true);
    bmp->bind("exchange1", "queue2", "new.sport.#", true);
    bmp->bind("exchange1", "queue3", "new.gossip.pop", true);
    bmp->bind("exchange2", "queue1", "new.music.football", true);
    bmp->bind("exchange2", "queue2", "new.music.pop", true);
    bmp->bind("exchange2", "queue3", "new.gossip.#", true);

    ASSERT_EQ(bmp->size(), 6);
}
TEST(binding_test, select_test)
{
    ASSERT_EQ(bmp->exists("exchange1","queue1"), true);
    ASSERT_EQ(bmp->exists("exchange1","queue2"), true);
    ASSERT_EQ(bmp->exists("exchange1","queue3"), true);
    ASSERT_EQ(bmp->exists("exchange2","queue1"), true);
    ASSERT_EQ(bmp->exists("exchange2","queue2"), true);
    ASSERT_EQ(bmp->exists("exchange2","queue3"), true);
    ns_kkymq::Binding::ptr bp = bmp->getBinding("exchange1","queue1");
    ASSERT_NE(bp.get(),nullptr);
    ASSERT_EQ(bp->_exchange_name, "exchange1");
    ASSERT_EQ(bp->_msgqueue_name, "queue1");
    ASSERT_EQ(bp->_binding_key, "new.music.#");
}
TEST(binding_test, select_exchange_test)
{
    ns_kkymq::MsgQueueBindingMap mqbm = bmp->getExchangeBindings("exchange1");
    ASSERT_EQ(mqbm.size(),3);
    ASSERT_NE(mqbm.find("queue1"),mqbm.end());
    ASSERT_NE(mqbm.find("queue2"),mqbm.end());
    ASSERT_NE(mqbm.find("queue3"),mqbm.end());
}
TEST(binding_test,remove_queue_test)
{
    bmp->removeMagQueueBindings("queue1");
    ASSERT_EQ(bmp->exists("exchange1","queue1"),false);
    ASSERT_EQ(bmp->exists("exchange2","queue1"),false);
}

TEST(binding_test,remove_exchange_test)
{
    bmp->removeExchangeBindings("exchange1");
    ASSERT_EQ(bmp->exists("exchange1","queue1"),false);
    ASSERT_EQ(bmp->exists("exchange1","queue2"),false);
    ASSERT_EQ(bmp->exists("exchange1","queue3"),false);

}

TEST(binding_test,remove_single_test)
{
    ASSERT_EQ(bmp->exists("exchange2","queue2"),true);
    bmp->unBind("exchange2","queue2");
    ASSERT_EQ(bmp->exists("exchange2","queue2"),false);

}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new BindingTest);
    RUN_ALL_TESTS();
    return 0;
}